package com.yoomiito.app.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.utils.DensityUtil

class TipPop(val context: Context, view: View, var content: String) : PopupWindow(context) {

    init {
        val mTipView = LayoutInflater.from(context).inflate(R.layout.small_tip_2, null, false)
        val tipTv = mTipView.findViewById<TextView>(R.id.tip)
        val iv = mTipView.findViewById<ImageView>(R.id.indicatorIv)
        tipTv.text = content
        isOutsideTouchable = true
        contentView = mTipView
        setBackgroundDrawable(ColorDrawable(Color.parseColor("#00000000")))
        var x = 0
        var y = 0
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        val screenWidth = App.mWidthPx
        val viewX = location[0]
        val viewY = location[1]
        val viewW = view.width
        val viewH = view.height
        val tipWidth = viewWidth(mTipView)
        val ivWidth = viewWidth(iv)
        y = viewY + viewH + DensityUtil.dp2px(5f)
        when {
            viewX < tipWidth / 2 -> {
                x = viewX + viewW / 2 - DensityUtil.dp2px(10f)
                val lp = iv.layoutParams as LinearLayout.LayoutParams
                lp.leftMargin = DensityUtil.dp2px(10f)
                iv.layoutParams = lp
                showAtLocation(view, Gravity.NO_GRAVITY, x, y)
            }
            screenWidth - (viewX + viewW) < tipWidth / 2 -> {
                x = viewX + viewW + DensityUtil.dp2px(10f) - tipWidth
                val lp = iv.layoutParams as LinearLayout.LayoutParams
                lp.leftMargin = viewX + viewW/2-ivWidth/2-x
                iv.layoutParams = lp
                showAtLocation(view, Gravity.NO_GRAVITY, x, y)
            }
            else -> {
//                x = viewX - (tipWidth / 2 + viewW / 2) // x为view右上角的坐标吗？
//                val lp = iv.layoutParams as LinearLayout.LayoutParams
//                lp.gravity = Gravity.CENTER
//                iv.layoutParams = lp
//                showAtLocation(view, Gravity.NO_GRAVITY, x, y)
                val lp = iv.layoutParams as LinearLayout.LayoutParams
                lp.gravity = Gravity.CENTER
                iv.layoutParams = lp
                y = DensityUtil.dp2px(5f)
                showAsDropDown(view,-(tipWidth / 2+viewW / 2),y)
            }
        }

    }

    private fun viewWidth(view: View): Int {
        // 1、UNSPECIFIED，不限定。意思就是，子View想要多大，我就可以给你多大，你放心大胆的measure吧，不用管其他的。也不用管我传递给你的尺寸值。（其实Android高版本中推荐，只要是这个模式，尺寸设置为0）
        //
        // 2、EXACTLY，精确的。意思就是，根据我当前的状况，结合你指定的尺寸参数来考虑，你就应该是这个尺寸，具体大小在MeasureSpec的尺寸属性中，自己去查看吧，你也不要管你的content有多大了，就用这个尺寸吧。
        //
        // 3、AT_MOST，最多的。意思就是，根据我当前的情况，结合你指定的尺寸参数来考虑，在不超过我给你限定的尺寸的前提下，你测量一个恰好能包裹你内容的尺寸就可以了。
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        return view.measuredWidth;
    }
}